<template>
  <div class="app-container">
    <el-row type="flex" align="center" :gutter="20">
      <el-form class="filterData-block" label-position="left" :inline="true" label-width="auto">
        <el-form-item label="姓名:">
          <el-input
            placeholder="姓名"
            v-model="filterData.name"
            maxlength="20"
            size="small"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label="人员类别:">
          <el-cascader
           v-model="filterData.type"
            clearable
            placeholder="选择人员类别"
            :options="typeOption"
            size="small"
            :props="{value:'name',label:'name'}"
            filterDataable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="参加职业技能培训状态:">
          <el-cascader
            v-model="filterData.isTrain"
            clearable
            placeholder="请选择"
            :options="isTrainOption"
            size="small"
            :props="{value:'name',label:'name'}"
            filterDataable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="是否需要技能培训:">
          <el-cascader
            v-model="filterData.isNeed"
            clearable
            placeholder="请选择"
            :options="isNeedOption"
            size="small"
            :props="{value:'name',label:'name'}"
            filterDataable
          ></el-cascader>
        </el-form-item>

        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="reset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-search" type="primary" @click="refreshTable">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" icon="el-icon-download" type="primary" @click="exportRecord">导出</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-table v-loading="tableConf.loading" :stripe="true" :border="true" :data="tableData">
        <el-table-column align="center" prop="id" label="ID"/>
        <el-table-column align="center" prop="isLocal" label="本县劳动力/非本县劳动力" width="240px"/>
        <el-table-column align="center" prop="localAddressText" label="本县劳动力地址"/>
        <el-table-column align="center" prop="notLocalAddress" label="非本县劳动力户籍所在地"/>
        <el-table-column align="center" prop="name" label="姓名"/>
        <el-table-column align="center" prop="age" label="年龄"/>
        <el-table-column align="center" prop="sex" label="性别"/>
        <el-table-column align="center" prop="nationId" label="民族"/>
        <el-table-column align="center" prop="mobile" label="联系电话"/>
        <el-table-column align="center" prop="type" label="人员类型"/>
        <el-table-column align="center" prop="isTrain" label="参加过职业技能培训么"/>
        <el-table-column align="center" prop="isNeed" label="是否需要技能培训提高自己"/>
        <el-table-column align="center" prop="trainWish" label="培训意愿"/>
        <el-table-column align="center" prop="career" label="目前从事职业"/>
        <el-table-column align="center" prop="workWish" label="就业意愿"/>
      </el-table>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-pagination
        :page-sizes="tableConf.sizes"
        :page-size="tableConf.size"
        :total="tableConf.total"
        :current-page="tableConf.page"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
  </div>
</template>

<script>
import TrainCodeDetail from '@/components/TrainCode/Detail'
import {
  getTrainInvestigateListApi,
  exportTrainInvestigateApi
} from '@/api/questionnaire'
import { createFile } from '@/utils/utils'

export default {
  components: { TrainCodeDetail },
  data() {
    return {
      typeOption:[
        {
          id:1,
          name:'高校毕业生'
        },
        {
          id:2,
          name:'退役军人'
        },
        {
          id:3,
          name:'残疾人'
        },
        {
          id:4,
          name:'低保户'
        },
        {
          id:5,
          name:'其他'
        }
      ],
      isTrainOption:[
        {
          id: 1,
          name: '参加过'
        }, {
          id: 2,
          name: '没参加过'
        }, {
          id: 3,
          name: '了解一点'
        },
      ],
      isNeedOption:[
        {
          id: 1,
          name: '不需要'
        }, {
          id: 2,
          name: '需要'
        }, {
          id: 2,
          name: '可能需要'
        }, {
          id: 2,
          name: '急需要'
        }, {
          id: 2,
          name: '急迫需要'
        },

      ],

      tableData: [],
      tableConf: {
        page: 1,
        sizes: [10, 20, 50, 100],
        total: 0,
        size: 10,
        loading: false
      },
      visible: {//控制弹窗显示
        0: false
      },
      detailId: null,
      keyword: '',//关键字查询
      filterData: {
        name: '',
        type:[],
        isTrain: [],
        isNeed: [],
      },
      placeTypeLoading: true,
      trainTypeOption: []
    }
  },
  mounted() {
    this.getTableList()
  },
  methods: {
    downloadCode(url) {
      window.open(url)
    },
    openRecord(sid) {
      //新标签页打开列表
      this.$router.push({
        name: 'TrainCustomer',
        query: {
          sid: sid
        }
      })
    },
    //打开添加/编辑页
    openDetail(id = null) {
      this.detailId = id
      this.visible[0] = true
    },
    //关闭指定弹窗并刷新页面
    closeDialog(index) {
      this.visible[index] = false
      this.getTableList()
    },
    // 每页显示数量change
    handleSizeChange(e) {
      this.tableConf.size = e
      this.getTableList()
    },
    // 当前页change
    handleCurrentChange(e) {
      this.tableConf.page = e
      this.getTableList()
    },
    //获取列表
    getTableList() {
      this.tableConf.loading = true
      const type = this.filterData.type.length === 0 ? '': this.filterData.type[0];
      const isTrain = this.filterData.isTrain.length === 0 ? '' : this.filterData.isTrain[0];
      const isNeed = this.filterData.isNeed.length === 0 ? '' : this.filterData.isNeed[0];
      getTrainInvestigateListApi({
        page: this.tableConf.page,
        limit: this.tableConf.size,
        name: this.filterData.name,
        type: type,
        isTrain: isTrain,
        isNeed: isNeed,
      }).then((res) => {
        this.tableData = res.list
        this.tableConf.total = res.count
      }).finally(() => {
        this.tableConf.loading = false
      })
    },
    exportRecord(){
      const type = this.filterData.type.length === 0 ? '': this.filterData.type[0];
      const isTrain = this.filterData.isTrain.length === 0 ? '' : this.filterData.isTrain[0];
      const isNeed = this.filterData.isNeed.length === 0 ? '' : this.filterData.isNeed[0];
      this.$confirm('确定导出当前查询条件的数据吗','提示').then(()=>{
        const loading=this.$loading({ text: '处理中，请勿关闭页面' })
        exportTrainInvestigateApi({
          name: this.filterData.name,
          type: type,
          isTrain: isTrain,
          isNeed: isNeed,
        }).then((res)=>{
          if(!createFile(res,'问卷调查列表')){
            this.$message.error('未知的文件格式');
          }
        }).finally(()=>{
          loading.close();
        })
      })
    },
    refreshTable() {
      this.tableConf.page = 1
      this.getTableList()
    },
    reset(){
      this.filterData.age = ''
      this.filterData.sex = ''
      this.filterData.education = ''
      this.filterData.domicile = ''
      this.filterData.workIndustry = ''
      this.getTableList()
    }
  }
}
</script>
<style lang="scss" scoped>
.filterData-block {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
